﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

//工作经验
function expValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var msg = validateCustomerDateRange(
       parseInt($("#f_exp_FromYear_" + tag).val()),
       parseInt($("#f_exp_FromMonth_" + tag).val()),
       parseInt($("#f_exp_ToYear_" + tag).val()),
       parseInt($("#f_exp_ToMonth_" + tag).val()));
    var flag = true;
    if (msg) {
        setErrorMessage($("#f_exp_ToMonth_" + tag), msg);
        flag = false;
    }
    else {
        clearErrorMessage($("#f_exp_ToMonth_" + tag));
    }  
      
    if (!validateRequiredFormItem('#f_exp_EmployerName_' + tag, ''))//雇主名不能为空
        flag = false;
    //if( !validateCustomerFormItem( $("#f_exp_EmployerPhone_" + tag) ) )//联系电话是否正确
        //flag = false;  
    if (!validateRequiredFormItem('#f_exp_PositionTitle_' + tag, ''))//职位不能为空
        flag = false;
 

    return flag;
}

//项目经验
function proValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var msg = validateCustomerDateRange(
       parseInt($("#f_pro_FromYear_" + tag).val()),
       parseInt($("#f_pro_FromMonth_" + tag).val()),
       parseInt($("#f_pro_ToYear_" + tag).val()),
       parseInt($("#f_pro_ToMonth_" + tag).val()));
    var flag = true;
    if (msg) {
        setErrorMessage($("#f_pro_ToMonth_" + tag), msg);
        flag = false;
    }
    else {
        clearErrorMessage($("#f_pro_ToMonth_" + tag));
    }

    if (!validateRequiredFormItem('#f_pro_ProjectName_' + tag, ''))//项目名称
        flag = false;
    
    //    if (!$("#f_pro_SummaryText_" + tag).val()) {
    //        setErrorMessage($("#f_pro_SummaryText_" + tag), ''); //项目描述
    //        flag = false;
    //    }

    if ($("#f_pro_SummaryText_" + tag).val().lenght > 500) {
        setErrorMessage($("#f_pro_SummaryText_" + tag), '项目描述内容不能超过500个字符'); //项目描述
        flag = false;
    }
    else {
        clearErrorMessage($("#f_pro_SummaryText_" + tag));
    }
    return flag;
}

//教育
function eduValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var msg = validateCustomerDateRange(
       parseInt($("#f_edu_FromYear_" + tag).val()),
       parseInt($("#f_edu_FromMonth_" + tag).val()),
       parseInt($("#f_edu_ToYear_" + tag).val()),
       parseInt($("#f_edu_ToMonth_" + tag).val()));
    var flag = true;
    if (msg) {
        setErrorMessage($("#f_edu_ToMonth_" + tag), msg);
        flag = false;
    }
    else {
        clearErrorMessage($("#f_edu_ToMonth_" + tag));
    }
    
    if (!validateRequiredFormItem('#f_edu_SchoolName_' + tag, ''))//学校
        flag = false;
    if (!validateRequiredFormItem('#f_edu_LearningWayID_' + tag, ''))//学习形式
        flag = false;
    if (!validateRequiredFormItem('#f_edu_EduMaJorName_' + tag, ''))//专业
        flag = false;
    if (!validateRequiredFormItem('#f_edu_EduDegreeLevelID_' + tag, ''))//所获学历
        flag = false;
    

    return flag;
}

//培训
function traValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var msg = validateCustomerDateRange(
       parseInt($("#f_tra_FromYear_" + tag).val()),
       parseInt($("#f_tra_FromMonth_" + tag).val()),
       parseInt($("#f_tra_ToYear_" + tag).val()),
       parseInt($("#f_tra_ToMonth_" + tag).val()));
    var flag = true;

    if (msg) {
        setErrorMessage($("#f_tra_ToMonth_" + tag), msg);
        flag = false;
    }
    else {
        clearErrorMessage($("#f_tra_ToMonth_" + tag));
    }
    
    if (!validateRequiredFormItem('#f_tra_TrainerName_' + tag, ''))//培训机构
        flag = false;
    if (!validateRequiredFormItem('#f_tra_TrainerAddress_' + tag, ''))//培训地点
        flag = false;
    if (!validateRequiredFormItem('#f_tra_Lesson_' + tag, ''))//课程
        flag = false;
    if (!validateRequiredFormItem('#f_tra_Centification_' + tag, ''))//所获证书
        flag = false;

    return flag;
}

//语言
function lanValidate(index,type) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    
    var flag = true;
    if (type == 0) {
        if (!validateRequiredFormItem('#f_eng_ENLevelID_0', ''))//英语等级
            flag = false;
        if (!validateNumerice("#f_eng_ENScore_0" ))
            flag = false;
        if (!validateNumerice("#f_eng_TOEFL_0"))
            flag = false;
        if (!validateNumerice("#f_eng_GRE_0" ))
            flag = false;
    }
    else {
        if (!validateRequiredFormItem('#f_lan_LanguageClassID_' + tag, ''))//其他语种
            flag = false;
    }

    return flag;
}

//skill
function skillValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var flag = true;
    if (!validateRequiredFormItem('#f_skill_SkillName_' + tag, ''))//技能名称
        flag = false;
        
    if (!validateNumerice("#f_skill_UsedTimes_" + tag) ) {
        flag = false;
    }
     
    return flag;
}

//certificate
function certificationValidate(index) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var flag = true;
    if (!validateRequiredFormItem('#f_certification_CertificationTitle_' + tag, ''))//技能名称
        flag = false;
    if( !validateCustomerFormItem( $("#f_certification_EffectiveDate_" + tag) ) )
        flag = false;
        
    return flag;
}

//strengths
function strengthsValidate(index) {
    return true;   
}

//attachment
function attachmentValidate(index,edit) {
    var tag = '';
    if (index || index == 0)
        tag = index;
    var flag = true;
    if (!validateRequiredFormItem('#f_attachment_AttacheName_' + tag, ''))//附件名称
        flag = false;
    /*
    if (!edit) {
        if (!validateRequiredFormItem('#f_attachment_AttachFile_' + tag, ''))//上传文件
            flag = false;
    }
    */
    return flag;
}

function validateRequiredFormItem(id, msg) {
    
    if (!$(id).val()) {
        setErrorMessage($(id), msg);
        return false;
    }
    else {
        clearErrorMessage($(id));
    }
    return true;
}

function validateNumerice(id) {
    return validateWithReg("^(\\d)+$", id, '必须为数字');
}

function validateWithReg(exp, id, msg,required) {
    var reg = new RegExp(exp);
    var ctrl = $(id);
    if (required && !ctrl.val()) {
        setErrorMessage($(ctrl), msg);
        return false;
    }
    
    if (ctrl.val() && !reg.exec(ctrl.val())) {
        setErrorMessage($(ctrl), msg);
        return false;
    }
    else {
        clearErrorMessage($(ctrl));
        return true;
    }
}

function clearErrorMessage(ctrl) {
    
    var parent = ctrl.parent();
    var item = ctrl.parent().children(".clew").html('');
}

function setErrorMessage(ctrl, msg) {
    var parent = ctrl.parent();
    var item = parent.children(".clew").eq(0);
    if (!item || item.size() == 0) {
        ctrl.after('<span class="clew" ></span>');
        item = ctrl.next();
    }
    if (!msg || msg.length == 0)
        msg = '请填写该项';
    item.html("<span>" + msg + "</span>");
    //item.attr("class",'error');
    return false;
}

//验证日期
function validateCustomerDateRange(fromYear, fromMonth, toYear, toMonth) {
    var d = new Date();
    var cy = d.getFullYear();
    var cm = d.getMonth() + 1;

    if (!fromYear)
        return "开始年份不能为空";
    if (!fromMonth)
        return "开始月份不能为空";
    if (fromYear > cy)
        return "开始年份不能大于当前年份";
    if (fromYear == cy && fromMonth > cm)
        return "开始日期不能大于当前日期";
    if (toYear>0 && fromYear > toYear)
        return "结束日期不能早于开始日期";
    if (toYear>0 && fromYear == toYear && fromMonth > toMonth)
        return "结束日期不能早于开始日期";
}

function validateUserBaseInfo(container) {
    var items = $(container).find("input[validate],select[validate]"); 
    
}

//reg2 validate

function validateForms(name) {
    
    var containers = $("." + name + ">div");
    var flag = true;
    for (var i = 0; i < containers.size(); i++) {
        if( !validateForm(containers.eq(i)) )
            flag = false;
    }
    if (!flag) {
        $("." + name).parent().show();
    }
    return flag;
}

function validateFormDataRange(container) {
    var item = $(container).find("select.weightyear");
    if( item.size()  == 0 )
        return true;
    var msg = validateCustomerDateRange(
        parseInt(item.eq(0).val()),
        parseInt($(container).find("select.weightmonth").eq(0).val()),
        parseInt(item.eq(1).val()),
        parseInt($(container).find("select.weightmonth").eq(1).val())
    );
    if (msg && msg.length > 0) {
        setErrorMessage(item.eq(0), msg);
        return false;
    }
    clearErrorMessage(item.eq(0));
    return true;
}

function validateForm(container) {
    
    if (!formInputedValue(container))
        return true;
    var items = $(container).find("input[validate],select[validate],textarea[validate]");
    var flag = true;
    if (!validateFormDataRange(container))
        flag = false;
    for (var i = 0; i < items.size(); i++) {
        var item = items.eq(i);
        if (!validateCustomerFormItem(item))
            flag = false;
    }
    
    return flag;
}

function validateCustomerFormItem(item) {
	
    var flag = true;
    var vals = item.attr("validate").split(':');
    var msg = '';
    if (vals.length > 1)
        msg = vals[vals.length - 1];
    if (vals[0] == "required") {
        if (!validateRequiredFormItem(item, msg))
            flag = false;
    }
    else if (vals[0] == "numeric") {
        if (!validateNumerice(item))
            flag = false;
    }
    else if (vals[0] == "regex") {

        if (!validateWithReg(vals[1], item, vals[3], vals[2] == "true")) {
            flag = false;
        }
    }
    else if (vals[0] == "fun") {
        var required = vals[2] == "true";
        var result = eval(vals[1] + "(item,required)");
        if (result && result.length > 0)
        { setErrorMessage(item, result); flag = false; }
        else
        { clearErrorMessage(item) };
    }
    return flag;
}

function validateDate(ctrl, required) {
    
    var val = $(ctrl).val();
    if (!required && ( !val || val.length == 0))
        return null;
        
    var reg = /^((((((0[48])|([13579][26])|([2468][048]))00)|([0-9][0-9]((0[48])|([13579][26])|([2468][048]))))-02-29)|(((000[1-9])|(00[1-9][0-9])|(0[1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))-((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30))|(((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-8]))))))$/i;
    if (reg.exec(val))
        return null;
    else
        //return '格式错误';
    	return '格式: 1997-10-01';
}

function validateTextAreaLength(ctrl, required) {
	var val = $(ctrl).val();
    if (!required && ( !val || val.length == 0))
        return null;
    
    if ( val.length < 5000 )
        return null;
    else
    	return "不能超过5000个汉字";
}

function formInputedValue(container) {
    var items = $(container).find("input,select,textarea");
    
    for (var i = 0; i < items.size(); i++) {
        var item = items.eq(i);
        if (item.val())
            return true;
    }
    
    return false;
}


/*  |xGv00|db8b1341503c96c0e0d868b214313417 */